package com.jeasonzhao.report.vml.test;

import com.jeasonzhao.report.vml.base.BaseElement;
import com.jeasonzhao.report.vml.base.Color;
import com.jeasonzhao.report.vml.base.VmlElement;
import com.jeasonzhao.report.vml.chart.BaseGraph;
import com.jeasonzhao.report.vml.chart.Chart;
import com.jeasonzhao.report.vml.chart.GridGraph;
import com.jeasonzhao.report.vml.chart.data.Element;
import com.jeasonzhao.report.vml.chart.data.Series;
import com.jeasonzhao.report.vml.chart.enums.NodeTypes;
import com.jeasonzhao.report.vml.chart.enums.RuleTypes;
import com.jeasonzhao.report.vml.chart.enums.SeriesTypes;


public class FileTestHelper
{
    public FileTestHelper()
    {
        super();
    }

    private static String getHtmlHead()
    {
        String strHead = " <html xmlns:v='urn:schemas-microsoft-com:vml'>\r\n" +
            " <head>\r\n" +
            "<title>Thinking in VML</title>\r\n" +
            "<style>\r\n" +
            "v\\:* { behavior: url(#default#VML); }\r\n" +
            "</style>\r\n" +
            "</head>\r\n" +
            "<body topmargin=1 leftmargin=1 >";
        return strHead;
    }

    public static void writeFile(VmlElement vmlacc,String strFileName)
    {
        try
        {
            writeFile(strFileName,
                      getHtmlHead() +
                      "\r\n" +
                      vmlacc.getScript());
            System.out.println("完成");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public static void writeFile(BaseGraph c,boolean bAsInTable,String strFileName)
    {
        try
        {
            String strScripts = "<v:Group CoordSize=\"" + c.getChartSize() + "\" " +
                "style=\"Height:100%;Width:100%;position:relative;background-color:" + Color.WhiteSmoke + "\">" +
                "\r\n" + c.getScript() +
                "\r\n</v:Group>";
            if(bAsInTable)
            {
                writeFile(strFileName,
                          getHtmlHead() +
                          strScripts
                    );

            }
            else
            {
                writeFile(strFileName,
                          getHtmlHead() +
                          "\r\n<table border=1 width='100%' cellpadding=0 cellspacing=0>" +
                          "\r\n<tr><td>AAA</td><td>AAA</td><td>AAA</td></tr>\r\n" +
                          "<tr><td>测试单元格12</td>" +
                          "<td width='600px' height='600px'>\r\n" +
                          strScripts +
                          "\r\n</td><td>宽度和高0</td></tr>\r\n" +
                          "<tr><td>测试单元格1</td><td>测试单元格</td><td>测试单元格</td></tr></table>\r\n");
            }
            System.out.println("完成");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public static void writeFile(BaseElement c,boolean bAsInTable,String strFileName)
    {
        try
        {
            String strScripts = c.getScript();
            if(bAsInTable)
            {
                writeFile(strFileName,
                          getHtmlHead() +
                          strScripts
                    );

            }
            else
            {
                writeFile(strFileName,
                          getHtmlHead() +
                          "\r\n<table border=1 width='100%' cellpadding=0 cellspacing=0>" +
                          "\r\n<tr><td>AAA</td><td>AAA</td><td>AAA</td></tr>\r\n" +
                          "<tr><td>测试单元格12</td>" +
                          "<td width='600px' height='600px'>\r\n" +
                          strScripts +
                          "\r\n</td><td>宽度和高0</td></tr>\r\n" +
                          "<tr><td>测试单元格1</td><td>测试单元格</td><td>测试单元格</td></tr></table>\r\n");
            }
            System.out.println("完成");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public static void writeFile(String strFileName,String str)
        throws Exception
    {
        java.io.File file = new java.io.File(strFileName == null ? "d:\\temp2.htm" : strFileName);
        if(!file.exists())
        {
            file.createNewFile();
        }
        java.io.BufferedWriter io = new java.io.BufferedWriter(new java.io.FileWriter(file));
        io.write(str);
        io.flush();
        io.close();
    }

    public static Series addSeries(
        GridGraph graph,
        String strSeriesName,
        SeriesTypes graphType,
        NodeTypes nodeType,
        RuleTypes ruleType,
        int nMaxValue,
        int nValuesCount,
        boolean isAddNegValues)
    {
        Series s3 = new Series();
        s3.setGraphType(graphType);
        s3.setNodeType(nodeType);
        s3.setRuleType(ruleType);
        s3.setTitle(strSeriesName);
        for(int n = 0;n < nValuesCount;n++)
        {
            for(int k = 0;k <= n;k++)
            {
                Math.random();
            }
            double lf = (int) (Math.random() * 100000) % 2 == 0 ?
                Math.random() * nMaxValue :
                0 - Math.random() * nMaxValue;
            if(isAddNegValues == false)
            {
                lf = Math.random() * nMaxValue;
            }
            s3.addElement(new Element(lf,"2007年" + (n + 1) + "月"));
        }
        graph.addSeries(s3);
        return s3;
    }

    /**
     * writeFile
     *
     * @param chart Chart
     * @param b boolean
     * @param object Object
     */
    public static void writeFile(Chart c,boolean bAsInTable,String strFileName)
    {
        try
        {
            String strScripts = c.getScript();
            if(bAsInTable)
            {
                writeFile(strFileName,
                          getHtmlHead() +
                          strScripts
                    );

            }
            else
            {
                writeFile(strFileName,
                          getHtmlHead() +
                          "\r\n<table border=1 width='100%' cellpadding=0 cellspacing=0>" +
                          "\r\n<tr><td>AAA</td><td>AAA</td><td>AAA</td></tr>\r\n" +
                          "<tr><td>测试单元格12</td>" +
                          "<td width='600px' height='600px'>\r\n" +
                          strScripts +
                          "\r\n</td><td>宽度和高0</td></tr>\r\n" +
                          "<tr><td>测试单元格1</td><td>测试单元格</td><td>测试单元格</td></tr></table>\r\n");
            }
            System.out.println("完成");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

    }

}
